<!-- src/algorithms/length_conversion.vue -->
<template>
    <div class="length-conversion">
      <h2>abc换工具</h2>
      <el-form :model="form">
        <el-form-item label="输入米数">
          <el-input v-model="form.meters" placeholder="请输入米数" />
        </el-form-item>
        <el-form-item label="选择单位">
          <el-select v-model="form.unit" placeholder="请选择单位">
            <el-option label="千米" value="kilometers"></el-option>
            <el-option label="厘米" value="centimeters"></el-option>
            <el-option label="毫米" value="millimeters"></el-option>
            <el-option label="英寸" value="inches"></el-option>
            <el-option label="英尺" value="feet"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="convert">转换</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
      <div v-if="result !== null" class="result">
        <h3>转换结果: {{ result }} {{ form.unit }}</h3>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        form: {
          meters: '',
          unit: '',
        },
        result: null,
      };
    },
    methods: {
      back(){
        this.$router.push('/tools')
      },

      convert() {
        const meters = parseFloat(this.form.meters);
        if (isNaN(meters)) {
          this.result = '请输入有效的数字';
          return;
        }
  
        switch (this.form.unit) {
          case 'kilometers':
            this.result = meters / 1000;
            break;
          case 'centimeters':
            this.result = meters * 100;
            break;
          case 'millimeters':
            this.result = meters * 1000;
            break;
          case 'inches':
            this.result = meters * 39.3701;
            break;
          case 'feet':
            this.result = meters * 3.28084;
            break;
          default:
            this.result = '请选择一个单位';
        }
      },
    },
  };
  </script>
  
  <style scoped>
  .length-conversion {
    padding: 20px;
  }
  
  .result {
    margin-top: 20px;
    font-size: 18px;
    font-weight: bold;
  }
  </style>
  